import Vue from 'vue'
import Vuex from 'vuex'
import { createLogger } from 'vuex'
// import createPersistedState from 'vuex-persistedstate'
import {shop_favlist,shop_fav,shopcart_list,shopcart_clear,shopcart_add} from '../utils/api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    //collectlist:localStorage.getItem('vuex')?JSON.parse(localStorage.getItem('vuex')).collectlist : [],//收藏列表
    collectlist : [],
    //shopcartlist:localStorage.getItem('vuex')?JSON.parse(localStorage.getItem('vuex')).shopcartlist : [],
    shopcartlist : []
  },

  getters: {
     totalCount(state) {
            return state.shopcartlist.reduce((t, item) => {
              return t + item.count;
            }, 0)
          },
          totalPrice(state) {
            return state.shopcartlist.reduce((t, item) => {
              return t + item.count * item.min_price;
            }, 0).toFixed(2)
          },
  },
  mutations: {
    collect(state,payload){//收藏店铺
      state.collectlist.push( payload );
      localStorage.setItem('vuex',JSON.stringify(state));
      // console.log(payload);
      
      // console.log(state);     
    },
    cancel_collect(state,payload){//取消收藏店铺
      var index = state.collectlist.findIndex((item)=>{
        return item.mtWmPoiId == payload.mtWmPoiId;
      })
      if(index != -1){
        state.collectlist.splice( index,1 );
      }
      localStorage.setItem('vuex',JSON.stringify(state));
      // console.log(state);     
    },
    save_favlist(state,payload){//保存收藏列表
      state.collectlist = payload;
    },
    save_shopcartlist(state,payload){//保存购物车列表
      state.shopcartlist = payload;
    },
    clear(state){//清空购物车列表
      state.shopcartlist = [];
    },
    // shopcart_add(state,payload){//数量加1
    //   var index = state.shopcartlist.findIndex((item)=>{
    //     return item.id== payload.id;
    //   })
    //   if(index != -1){
    //     state.shopcartlist[index].count++;
    //   }else{
    //     var good = JSON.parse(JSON.stringify(payload))
    //     good.count = 1;
    //     state.shopcartlist.push(good);
    //   }
    // }
    cartadd(state, payload) {
      var res = state.shopcartlist.findIndex((item) => {
        return item.id == payload.id
      })
      if (res != -1) {//已经在购物车里面
        state.shopcartlist[res] + count+1;
      } else {//第一次加入购物车
        //深拷贝
        var good = JSON.parse(JSON.stringify(payload))
        good.count = 1;
        state.shopcartlist.push(good)
      }
    },
  },
  actions: {
    request_favlist( store ){//发请求，获取店铺列表
    //获取本地存储中的用户id
    var userid = JSON.parse(localStorage.getItem('userinfo' )).id;
    //发请求 获取当前用户的收藏列表
    shop_favlist({userid}).then((res)=>{
      // console.log(res.data.list);  
      store.commit('save_favlist',res.data.list)
    })
    },
    request_fav( store,payload ){
      //获取本地存储中的用户id
      var userid = JSON.parse(localStorage.getItem('userinfo' )).id;
      //发请求 收藏
      shop_fav({userid,shopid:payload.mtWmPoiId}).then((res)=>{
        if(res.data.code == 200){
          store.commit('collect',payload);//调用同步，收藏到vuex
        }
      })
      },
    request_shopcartlist(store){//发请求，购物车列表
    //获取本地存储中的用户id
    var userid = JSON.parse(localStorage.getItem('userinfo' )).id;
      shopcart_list( {userid} ).then((res)=>{
        //发请求，获取购物车列表
        if(res.data.code == 200){
          console.log("111111"+res.data.list);
          store.commit('save_shopcartlist',res.data.list);
        }
      })
    },
    request_shopcartclear(store){//发请求，清空购物车
    var userid = JSON.parse(localStorage.getItem('userinfo' )).id;
    shopcart_clear({userid}).then((res)=>{
      if(res.data.code == 200){
        store.commit('clear');
      }
    })
    },
    requery_add(store, payload) {//payload  商品对象
      let userid = JSON.parse(localStorage.getItem("userinfo")).id;//用户id
      shopcart_add({ userid, goodid: payload.id }).then((ok) => {
        if (ok.data.code == 200) {
          store.commit("cartadd", payload)
        }
      })
    },

  },
  modules: {
  },
  plugins: [createLogger()]
})
